package main


import (
	"fmt"
	"io"
	"mass-user/commons/message"
	"mass-user/commons/utils"
	"mass-user/server/processor"
	"net"
)

type Processor struct {
	Conn net.Conn
}


func (p Processor) ServerProcessMes(msg *message.Message) error {
	switch msg.Type {
	case message.LoginMessageType:
		// 处理登录
		userLogin := &processor.UserProcess{
			Conn: p.Conn,
		}
		err := userLogin.ServerProcessLogin(msg)
		return err
	case message.RegisterMessageType:
	// 处理注册
		userReg := processor.UserProcess{
			Conn: p.Conn,
		}
		err := userReg.ServerProcessReg(msg)
		return err
	default:
		fmt.Println("type error")
	}
	return nil
}


func  (p Processor) process() (err error) {
	for {
		tf := &utils.Transfer{
			Conn: p.Conn,
			Buf:  [8096]byte{},
		}
		msg, err := tf.ReadPkg()
		if err != nil {
			if err == io.EOF{
				fmt.Println("客户端退出！")
				return err
			}
			return err
		}
		fmt.Println(p.Conn.RemoteAddr(), msg)
		err = p.ServerProcessMes(&msg)
		if err != nil{
			return err
		}
		return nil
	}
}